﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp.Infrastructure
{
    public class ImageCreateHelper
    {
        /// <summary>
        /// 写文本
        /// </summary>
        /// <param name="text"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public static byte[] CreatePicture(string text, int width, int height)
        {
            String[] array = text.Split(new char[] { '\n' }, StringSplitOptions.None);
            if (array.Length > 1)
            {
                using (Bitmap img = new Bitmap(width, height))
                {
                    // _code = GetRandomCode();
                    // System.Web.HttpContext.Current.Session["vcode"] = _code;
                    using (Graphics g = Graphics.FromImage(img))
                    {
                        g.Clear(Color.White);//绘画背景颜色
                        g.DrawString(array[0], new Font("宋体", 14, FontStyle.Bold), Brushes.DarkRed, 5, 25);
                        for (int i = 1; i < array.Length; i++)
                        {
                            g.DrawString(array[0], new Font("宋体", 14, FontStyle.Bold), Brushes.DarkRed, 5, 25 + i * 20);
                        }
                    }

                    using (MemoryStream ms = new MemoryStream())
                    {
                        img.Save(ms, ImageFormat.Jpeg);

                        return ms.ToArray();
                    }
                }
            }
            else
            {
                using (Bitmap img = new Bitmap(width, height))
                {
                    // _code = GetRandomCode();
                    // System.Web.HttpContext.Current.Session["vcode"] = _code;
                    using (Graphics g = Graphics.FromImage(img))
                    {
                        g.Clear(Color.White);//绘画背景颜色
                        g.DrawString(array[0], new Font("宋体", 14, FontStyle.Bold), Brushes.Red, 5, 25);
                    }

                    using (MemoryStream ms = new MemoryStream())
                    {
                        img.Save(ms, ImageFormat.Jpeg);

                        return ms.ToArray();
                    }
                }
            }
        }

        private static Random r = new Random((int)DateTime.Now.ToFileTimeUtc());

        /// <summary>
        /// 创建验证码
        /// </summary>
        /// <param name="len"></param>
        /// <returns></returns>
        public static Tuple<Bitmap, string> GetBitmapCode(int len)
        {
            string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            string code = "";
            for (int i = 0; i < len; i++)
            {
                int rNumber = r.Next(0, str.Length - 1);
                code += str[rNumber].ToString();
            }

            //存储产生的Code值
            Bitmap bmp = new Bitmap(80, 20);//设置长度和宽度分别为80和20，bmp为画布
            Graphics g = Graphics.FromImage(bmp);//Bitmap是Image的子类，所以可以替换Image放置在函数中，g为画笔

            for (int i = 0; i < 4; i++)
            {
                Point p = new Point(i * 20, 0);//生成坐标点
                string[] fonts = { "微软雅黑", "宋体", "黑体", "隶书", "仿宋" };
                Color[] colors = { Color.Blue, Color.Black, Color.Red, Color.Green };
                g.DrawString(code[i].ToString(), new Font(fonts[r.Next(0, 5)], 15, FontStyle.Bold), new SolidBrush(colors[r.Next(0, 4)]), p);
            }
            //画干扰线，可以不要
            for (int i = 0; i < 5; i++)
            {
                Point p1 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
                Point p2 = new Point(r.Next(0, bmp.Width), r.Next(0, bmp.Height));
                g.DrawLine(new Pen(Brushes.Green), p1, p2);
            }

            return Tuple.Create(bmp, code);
        }
    }
}
